//
// Created by hsj on 2022/1/5.
//
#include <iostream>
#include "t_struct_func_pointer.h"

//结构与函数的一些测试代码
void test_struct_func_pointer(){
    using namespace std;
    travel_time t1{.hours = 3,.minites = 25};
    travel_time t2{1,10};

//    travel_time t = sum_copy(t1,t2);
//    show_time_copy(t);

    travel_time *t = sum_pointer(&t1,&t2);
    show_time_pointer(t);
}
//求总时间-复制副本
travel_time sum_copy(travel_time t1,travel_time t2){
    travel_time t;
    t.minites = (t1.minites + t2.minites) % MINITES_PER_HOURES;
    t.hours = (t1.hours + t2.hours) + (t1.minites + t2.minites) / MINITES_PER_HOURES;
    return t;
}
//显示时间-复制副本
void show_time_copy(travel_time t){
    using namespace std;
    //display houres minites
    cout<<"houres:"<<t.hours<<endl<<"minites:"<<t.minites<<endl;
}

//求总时间-地址
travel_time *sum_pointer(const travel_time *t1,const travel_time *t2){
    //不创建，那么就是local variable 栈变量，函数调用完成就消失
    //new 创建就是堆变量
    travel_time *t = new travel_time;
    (*t).minites = ((*t1).minites + (*t2).minites) % MINITES_PER_HOURES;
    t->hours = (t1->hours + t2->hours) + (t1->minites + t2->minites) / MINITES_PER_HOURES;
    return t;
}
//显示时间-地址
void show_time_pointer(const travel_time *t){
    using namespace std;
    //display houres minites
    cout<<"houres:"<<(*t).hours<<endl<<"minites:"<<t->minites<<endl;
    //释放内存
    delete t;
}